package club.xyes.pi4j.controller;

import club.xyes.pi4j.i2c.GpioIICDevice;
import club.xyes.pi4j.vo.BasicJsonResult;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 2018/11/8 Create by 郭文梁
 * SSD1306Controller
 * SSD1306驱动
 *
 * @author 郭文梁
 * @date 2018/11/8
 */
@RestController
@RequestMapping("/ssd")
public class SSD1306Controller {
    private GpioController controller;
    private GpioPinDigitalOutput sda;
    private GpioPinDigitalOutput scl;
    private GpioIICDevice device;

    @GetMapping("/init-gpio")
    public BasicJsonResult initGpio() {
        controller = GpioFactory.getInstance();
        sda = controller.provisionDigitalOutputPin(RaspiPin.GPIO_20);
        sda = controller.provisionDigitalOutputPin(RaspiPin.GPIO_21);
        return BasicJsonResult.ok();
    }

    @GetMapping("/init-ssd")
    public BasicJsonResult initSSD1306() {
        device = new GpioIICDevice(sda, scl);
        device.init();
        return BasicJsonResult.ok();
    }

    @GetMapping("/full")
    public BasicJsonResult full() {
        device.full();
        return BasicJsonResult.ok();
    }

    @GetMapping("/clear")
    public BasicJsonResult clear() {
        device.clear();
        return BasicJsonResult.ok();
    }
}
